package net.dgg.rz.common.utils.evaluatingExcel;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zyou on 2020/3/19.
 */
public class ReadExcel {
    public static void main(String[] args) throws IOException {
        importJsInfo();
    }

    public static void importJsInfo() throws IOException {
        String name ="1.xlsx";
        FileInputStream  f = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\北京地区客户属性评分以及调研表.xlsx"));
        //读取excel方法
        List<Map> map = readXls(f,name);
    }
    /**
     * 读取excel
     * @throws IOException
     */
    public static List<Map> readXls(InputStream inputStream, String fileName) throws IOException  {
        //判断是否是excel2007格式
        boolean isE2007 = false;
        if(fileName.endsWith("xlsx")){
            isE2007 = true;
        }
        //建立输入流
        InputStream input = inputStream;
        Workbook wb  = null;
        //根据文件格式(2003或者2007)来初始化
        if(isE2007){
            wb = new XSSFWorkbook(input);
        }else{
            wb = new HSSFWorkbook(input);
        }
        int sheetCount = wb.getNumberOfSheets();
        List<Map> papers = new ArrayList<>();
        for(int i=0;i<sheetCount;i++){
            //获得sheet
            Sheet sheet = wb.getSheetAt(i);
            papers.add(getSheetData(sheet));
        }
        return papers;
    }

    public static Map getSheetData(Sheet sheet){
        Map map = new HashMap();
        List<CellRangeAddress> cras = ImportSubject.getCombineCell(sheet);
        int count = sheet.getLastRowNum()+1;//总行数

        //题目
        List<Map> subjects = new ArrayList<>();
        Map subject = null;
        //获取试卷名称
        String paperName = sheet.getSheetName();
        String areaCode = sheet.getRow(0).getCell(0).getStringCellValue();
        //总列数
        for(int i = 2; i < count;i++){
            //选项
            List<Map> options = new ArrayList<>();
            Map option = null;
            Row row = sheet.getRow(i);
            subject = new HashMap();
            if(ImportSubject.getCellValue(row.getCell(1)) != "") {
                //获取第一列，试题名称
                String subjectName = row.getCell(1).getStringCellValue();
                subject.put("subjectName",subjectName);
                //判断如果是合并单元格
                if(ImportSubject.isMergedRegion(sheet,i,0)){
                    int lastRow = ImportSubject.getRowNum(cras,sheet.getRow(i).getCell(0),sheet);
                    for(;i<=lastRow;i++){
                        row = sheet.getRow(i);
                        //选项
                        option  = new HashMap();
                        option.put("optionName",ImportSubject.getCellValue(row.getCell(2)));
                        option.put("score",row.getCell(3).getNumericCellValue());
                        options.add(option);
                    }
                    i--;
                }else{
                    row = sheet.getRow(i);
                    //选项
                    option  = new HashMap();
                    option.put("optionName",ImportSubject.getCellValue(row.getCell(2)));
                    option.put("score",row.getCell(3).getNumericCellValue());
                    options.add(option);
                }
                subject.put("options",options);
                subjects.add(subject);
            }

        }
        map.put("paperName", paperName);
        map.put("areaCode", areaCode);
        map.put("subjects",subjects);
        return map;
    }
}
